<template>
  <div class="login">
    <!-- 卡片 -->
    <div class="login_box">
      <el-card class="box-card">
        <!-- 图片区 -->
        <div class="avatar_box">
          <img src="../../assets/images/2.jpg" />
        </div>
        <!-- 登录表单 -->
        <el-form
          :model="data.loginForm"
          ref="LoginFormRef"
          :rules="data.loginFormRules"
          label-width="0px"
          class="login_form"
        >
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input
              v-model="data.loginForm.username"
              prefix-icon="User"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              type="password"
              v-model="data.loginForm.password"
              prefix-icon="Lock"
              show-password
            >
            </el-input>
          </el-form-item>
          <!-- 按钮 -->
          <el-form-item>
            <div class="btns w100">
              <el-button type="primary" @click="denglu">登录</el-button>
              <el-button type="info" @click="chongzhi">重置</el-button>
            </div>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { reactive } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
const router = useRouter();

let data = reactive({
  loginForm: {
    username: "",
    password: "",
  },
  //表单验证规则
  loginFormRules: {
    username: [
      { required: true, message: "请输入登录名", trigger: "blur" },
      {
        min: 3,
        max: 10,
        message: "登录名长度在 3 到 10 个字符",
        trigger: "blur",
      },
    ],
    password: [
      { required: true, message: "请输入密码", trigger: "blur" },
      {
        min: 6,
        max: 15,
        message: "密码长度在 6 到 15 个字符",
        trigger: "blur",
      },
    ],
  },
});

let denglu = () => {
  if (
    data.loginForm.username == "admin" &&
    data.loginForm.password == "123456"
  ) {
    ElMessage.success("登录成功");
    // 将用户的token进行保存，例如用户的token为curry
    //保存token
    const token = "curry";
    window.localStorage.setItem("token", token);
    router.push("/menu");
  } else {
    ElMessage.error("用户名或密码错误");
  }
};

let chongzhi = () => {
  data.loginForm.username = "";
  data.loginForm.password = "";
};
</script>

<style scoped lang="scss">
.login {
  height: 100%;
  background-image: url("../../assets/images/1.jpg");
  background-size: 100% 100%;
}

.login_box {
  width: 450px;
  height: 280px;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 3px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 5px 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
  margin-right: 30px;
}

.el-card {
  background-color: rgba(255, 255, 255, 0.1);
}

.el-input {
  width: 90%;
  padding: 5px 0px;
}
</style>